const APPID = "wx2605fac48a714a5c";
const SECRET = "8f5788f30e52660f4ba840ed3859dd8f";
const axios = require('axios');
const WXBizDataCrypt = require('../../tool/WXBizDataCrypt');
exports.get_session_key = (req, res, next) => {
    //console.log('code',req.body.code)
    axios.get("https://api.weixin.qq.com/sns/jscode2session",
        {
            params: {
                appid:APPID,
                secret:SECRET,
                js_code:req.body.code,
                grant_type:"authorization_code"
            }
        }).then(response => {
            console.log(response.data)
            res.status(200).json(response.data)
        }).catch(error => {
            res.status(401).json(error)
        })/**/
}
exports.get_phone_number = (req, res, next) => {//解密获取手机号
    //console.log('body',req.body)
    try {
        var pc = new WXBizDataCrypt(APPID, req.body.sessionKey)
        var data = pc.decryptData(req.body.encryptedData , req.body.iv)
        res.status(200).json(data)
        //console.log('解密后 data: ', data)
    } catch (error) {
        res.status(401).json(error)
    }
}
exports.get_access_token = (req, res, next) => {
    axios.get("https://api.weixin.qq.com/cgi-bin/token",
        {
            params: {
                appid:APPID,
                secret:SECRET,
                grant_type:"client_credential"
            }
        }).then(response => {
            //console.log(response.data)
            res.status(200).json(response.data)
        }).catch(error => {
            res.status(401).json(error)
        })
}
exports.get_idcard = (req, res, next) => {//图片不宜超过100kb
    axios.get("https://reptile.akeyn.com/ocr/idcard",
        {
            params: {
                url:'http://dtifcf.natappfree.cc/node/images/card1.jpg' //该图片必须是外网可以访问
            }
        }).then(response => {
            //console.log(response.data)
            res.status(200).json(response.data)
        }).catch(error => {
            res.status(401).json(error)
        })
}